package com.common;

import java.util.HashMap;
import java.util.Map;

public class ThreadLocalVarUtil {

    private static final ThreadLocal<Map<String, Object>> threadLocalVar = new ThreadLocal<>();

    public static void put(String key, Object value) {
        Map<String, Object> threadLocalMap = threadLocalVar.get();
        if (null == threadLocalMap) {
            threadLocalMap = new HashMap<>();
            threadLocalVar.set(threadLocalMap);
        }
        threadLocalMap.put(key, value);
    }

    public static Object get(String key) {
        Map<String, Object> threadLocalMap = threadLocalVar.get();
        if (null == threadLocalMap) {
            return null;
        }
        return threadLocalMap.get(key);
    }

    public static boolean remove() {
        Map<String, Object> threadLocalMap = threadLocalVar.get();
        if (null != threadLocalMap) {
            threadLocalMap.clear();
        }
        threadLocalVar.remove();
        return true;
    }
}